package com.ch999.mobileoa.util;

import android.content.Context;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.ch999.mobileoa.MyApplication;
import com.sda.lib.util.Logs;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by scorpio on 15/12/25.
 */
public class Location {

    public AMapLocationClient client =null ;

    public Location(Context context) {
        client = new AMapLocationClient(context);
        AMapLocationClientOption option  = new AMapLocationClientOption();
        option.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        option.setNeedAddress(true);
        option.setOnceLocation(false);
        option.setWifiActiveScan(true);
        option.setMockEnable(false);
        option.setInterval(2000);
        client.setLocationOption(option);
    }


    public void getGPS() {
        client.startLocation();
        client.setLocationListener(new AMapLocationListener() {
            @Override
            public void onLocationChanged(AMapLocation amapLocation) {
                if (amapLocation != null) {
                    if (amapLocation.getErrorCode() == 0) {
                        //定位成功回调信息，设置相关消息
                        amapLocation.getLocationType();//获取当前定位结果来源，如网络定位结果，详见定位类型表
                        amapLocation.getLatitude();//获取纬度
                        amapLocation.getLongitude();//获取经度
                        amapLocation.getAccuracy();//获取精度信息
                        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        Date date = new Date(amapLocation.getTime());
                        df.format(date);//定位时间
                        amapLocation.getAddress();//地址，如果option中设置isNeedAddress为false，则没有此结果
                        amapLocation.getCountry();//国家信息
                        amapLocation.getProvince();//省信息
                        amapLocation.getCity();//城市信息
                        amapLocation.getDistrict();//城区信息
                        amapLocation.getRoad();//街道信息
                        amapLocation.getCityCode();//城市编码
                        amapLocation.getAdCode();//地区编码
                        Logs.Debug("定位=="+amapLocation.getLatitude() +"=="+amapLocation.getLongitude());
                        MyApplication.gps = amapLocation.getLatitude()+","+amapLocation.getLongitude();
                        client.stopLocation();
                        client.onDestroy();
                    } else {
                        //显示错误信息ErrCode是错误码，errInfo是错误信息，详见错误码表。
                        Logs.Debug("location Error, ErrCode:"
                                + amapLocation.getErrorCode() + ", errInfo:"
                                + amapLocation.getErrorInfo());
                    }
                }

            }
        });
    }
}
